﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DepartmentWork.Migrator.Data;
using DepartmentWork.Model;
using DepartmentWork.Platform.Controllers;

namespace DepartmentWork.Platform.Areas.SystemSettingArea.Controllers
{
    public class AuthorityController : BaseController
    {
        //
        // GET: /SystemSettingArea/Authority/

        public ActionResult Index(string flag = "", string flagInfo = "")
        {

            #region 设置分页

            var pageSize = this.Request["pageSize"] == null ? 10 : int.Parse(Request["pageSize"]);
            var pageIndex = this.Request["pageIndex"] == null ? 1 : int.Parse(Request["pageIndex"]);

            ViewData["pageIndex"] = pageIndex;
            ViewData["pageSize"] = pageSize;

            #endregion



            List<EMP_AUTHORITY> list = empAuthorityRepository.GetModel().ToList();

            EmpAuthorityViewModel model=new EmpAuthorityViewModel();
            
            model.ListOfAuthority=new List<EmpAuthority>();

            model.ListOfAuthority.AddRange((from item in list
                                      select new EmpAuthority
                                      {
                                          AUTHORITY_ID = item.AUTHORITY_ID,
                                          AUTHORITY_ILLUSTRATE = item.AUTHORITY_ILLUSTRATE,
                                          AUTHORITY_TITLE = item.AUTHORITY_TITLE
                                      }).ToList());

            ViewData["total"] = list.Count;

            List<ROLE_INFO> listOfRole=  roleRepository.GetModel().ToList();

            model.ListOfRole=new List<RoleViewModel>();

            model.ListOfRole.AddRange((from item in listOfRole
                                     select new RoleViewModel()
                                     {
                                         DESCRIPTION = item.DESCRIPTION,
                                         ROLE_ID = item.ROLE_ID,
                                         ROLE_NAME = item.ROLE_NAME,
                                         STATUE = item.STATUE,
                                         TEAMSPACE_ID = item.TEAMSPACE_ID
                                     }).ToList());


            return View(model);
        }

        [HttpPost]
        public ActionResult CreateAuthority(FormCollection collection)
        {
            try
            {
                string authorityName = collection["tb_AuthorityName"];
                string authorityContext = collection["tb_CreateAuthorityContext"];

                EMP_AUTHORITY model = new EMP_AUTHORITY()
                {
                    AUTHORITY_ID = Guid.NewGuid(),
                    AUTHORITY_ILLUSTRATE = authorityContext,
                    AUTHORITY_TITLE = authorityName
                };

                empAuthorityRepository.Insert(model);
            }
            catch (Exception ex)
            {
                return this.RedirectToAction
                    ("Index", "Authority", new
                    {
                        area = "SystemSettingArea",
                        flag = "操作失败",
                        flagInfo = ex.Message
                    });
            }

            return this.RedirectToAction
                    ("Index", "Authority", new
                    {
                        area = "SystemSettingArea",
                        flag = "操作成功",
                        flagInfo = ""
                    });
        }


        [HttpPost]
        public JsonResult GetAuthorityRoleInfo(string authorityId)
        {
            Guid aId=new Guid(authorityId);

            List<AUTHORITY_ROLE> list= authorityRoleRepository.GetModel(r => r.AUTHORITY_ID == aId).ToList();
            List<AuthorityRoleViewModel> model=new List<AuthorityRoleViewModel>();

            model.AddRange((from item in list
                            select new AuthorityRoleViewModel
                            {
                                AUTHORITY_ID = item.AUTHORITY_ID,
                                ROLE_ID = item.ROLE_ID,
                                AUTHORITY_ROLE_ID = item.AUTHORITY_ROLE_ID
                            }).ToList());

            return this.Json(model);
        }

        [HttpPost]
        public ActionResult BindingAuthorityRole(FormCollection collection)
        {
            try
            {
                string roleArrStr = collection["ck_RoleId"];

                Guid authId = new Guid(collection["hid_AuthorityId"]);

                List<AUTHORITY_ROLE> delAuthRoleList = new List<AUTHORITY_ROLE>();
                delAuthRoleList.AddRange(authorityRoleRepository.GetModel(r => r.AUTHORITY_ID == authId).ToList());
                for (int i = 0; i < delAuthRoleList.Count; i++)
                {
                    delAuthRoleList.Remove(delAuthRoleList[i]);
                }

                string[] roleArr = roleArrStr.Split(',').ToArray();

                for (int i = 0; i < roleArr.Length; i++)
                {
                    AUTHORITY_ROLE model = new AUTHORITY_ROLE();
                    model.AUTHORITY_ROLE_ID = Guid.NewGuid();
                    model.ROLE_ID = Convert.ToInt32(roleArr[i]);
                    model.AUTHORITY_ID = authId;

                    authorityRoleRepository.Insert(model);
                }
            }
            catch (Exception ex)
            {
                return this.RedirectToAction
                    ("Index", "Authority", new
                    {
                        area = "SystemSettingArea",
                        flag = "操作失败",
                        flagInfo = ex.Message
                    });
            }

            return this.RedirectToAction
                         ("Index", "Authority", new
                         {
                             area = "SystemSettingArea",
                             flag = "操作成功",
                             flagInfo = ""
                         });
        }

        public ActionResult DelAuthorityRole(string authorityId)
        {
            try
            {
                Guid authId = new Guid(authorityId);
                List<AUTHORITY_ROLE> delAuthRoleList = new List<AUTHORITY_ROLE>();
                delAuthRoleList.AddRange(authorityRoleRepository.GetModel(r => r.AUTHORITY_ID == authId).ToList());
                for (int i = 0; i < delAuthRoleList.Count; i++)
                {
                    delAuthRoleList.Remove(delAuthRoleList[i]);
                }
                EMP_AUTHORITY empAuth = empAuthorityRepository.GetModel(r => r.AUTHORITY_ID == authId).FirstOrDefault();
                empAuthorityRepository.Delete(empAuth);
            }
            catch (Exception ex)
            {
                return this.RedirectToAction
                    ("Index", "Authority", new
                    {
                        area = "SystemSettingArea",
                        flag = "操作失败",
                        flagInfo = ex.Message
                    });
            }
            return this.RedirectToAction
             ("Index", "Authority", new
             {
                 area = "SystemSettingArea",
                 flag = "操作成功",
                 flagInfo = ""
             });
        }


    }
}
